﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using TMDT.BusinessObjects;

namespace TMDT.BusinessLogic
{
    public class LoaiTaiKhoanManager
    {
        public bool Add(LoaiTaiKhoan entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spLoaiTaiKhoanInsert",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDLoaiTaiKhoan",
                                                                                             entity.IDLoaiTaiKhoan),
                                                                            new SqlParameter("@TenLoaiTaiKhoan",
                                                                                             entity.TenLoaiTaiKhoan),
                                                                            new SqlParameter("@Quyen", entity.Quyen),
                                                                        }
                                                    });
        }

        public bool Update(LoaiTaiKhoan entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spLoaiTaiKhoanUpdate",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDLoaiTaiKhoan",
                                                                                             entity.IDLoaiTaiKhoan),
                                                                            new SqlParameter("@TenLoaiTaiKhoan",
                                                                                             entity.TenLoaiTaiKhoan),
                                                                            new SqlParameter("@Quyen", entity.Quyen),
                                                                        }
                                                    });
        }

        public bool Remove(object id)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spLoaiTaiKhoanDelete",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDLoaiTaiKhoan", id)
                                                                        }
                                                    });
        }

        public List<LoaiTaiKhoan> GetLoaiTaiKhoan()
        {
            List<LoaiTaiKhoan> loaiTaiKhoan = new List<LoaiTaiKhoan>();
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText = "select * from LoaiTaiKhoan",
                                                         CommandType = CommandType.Text,
                                                     });
            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                loaiTaiKhoan.Add(LoaiTaiKhoan.Convert(row));
            }
            return loaiTaiKhoan;
        }

        public LoaiTaiKhoan GetLoaiTaiKhoan(object id)
        {
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText =
                                                             "select * from LoaiTaiKhoan where IDLoaiTaiKhoan = @IDLoaiTaiKhoan",
                                                         CommandType = CommandType.Text,
                                                         Parameter = new[]
                                                                         {
                                                                             new SqlParameter("@IDLoaiTaiKhoan", id),
                                                                         }
                                                     });
            return new LoaiTaiKhoan
                       {
                           IDLoaiTaiKhoan = new Guid(dtSet.Tables[0].Rows[0]["IDLoaiTaiKhoan"].ToString()),
                           Quyen = int.Parse(dtSet.Tables[0].Rows[0]["Quyen"].ToString()),
                           TenLoaiTaiKhoan = dtSet.Tables[0].Rows[0]["TenLoaiTaiKhoan"].ToString()
                       };
        }

        public LoaiTaiKhoan GetLoaiTaiKhoanByQuyen(int quyen)
        {
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText = "select * from LoaiTaiKhoan where Quyen = @Quyen",
                                                         CommandType = CommandType.Text,
                                                         Parameter = new SqlParameter[]
                                                                         {
                                                                             new SqlParameter("@Quyen",quyen)
                                                                         }
                                                     });
            return LoaiTaiKhoan.Convert(dtSet.Tables[0].Rows[0]);
        }
    }
}
